//
//  LJLClassificationPageViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Dark. All rights reserved.
//
#import "LTNetTool.h"
#import "UIImageView+WebCache.h"

#import "LJLClassificationPageViewController.h"
#import "LJLClassificationPageTVC.h"
#import "LJLClassificationPageModel.h"
#import "ListenNewsViewController.h"
@interface LJLClassificationPageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *leftIV;
@property (nonatomic, strong) UIImageView *midUpIV;
@property (nonatomic, strong) UIImageView *midDownIV;
@property (nonatomic, strong) UIImageView *rightUpIV;
@property (nonatomic, strong) UIImageView *rightDownIV;

@property (nonatomic, strong) UITableView *classificationPageTV;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *headerDataSource;

@end

@implementation LJLClassificationPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.headerDataSource = [NSMutableArray array];
    [self createView];
    [self createData];
}
- (void)createView
{
    self.classificationPageTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 139 * FITHEIGHT) style:UITableViewStylePlain];
    self.classificationPageTV.backgroundColor = [UIColor clearColor];
    self.classificationPageTV.delegate = self;
    self.classificationPageTV.dataSource = self;
    self.classificationPageTV.separatorStyle = 0;
    [self.view addSubview:_classificationPageTV];
    
    UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50 * FITHEIGHT)];
    foot.backgroundColor = [UIColor clearColor];
    self.classificationPageTV.tableFooterView = foot;
    
    [self.classificationPageTV registerClass:[LJLClassificationPageTVC class] forCellReuseIdentifier:@"LJLclassificationpageTVC"];
}
- (void)createData
{
    [LTNetTool GetNetWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/categories?channel=and-102-baiduPZ&device=android&picVersion=11&scale=2&statEvent=pageview%2Ftab%40%E5%8F%91%E7%8E%B0_%E5%88%86%E7%B1%BB" Body:nil Header:nil Response:0 Success:^(id result) {
        NSArray *array = [result objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            LJLClassificationPageModel *model = [[LJLClassificationPageModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
        }
        for (NSInteger i = 0; i < 5; i++) {
            [self.headerDataSource addObject:self.dataSource[i]];
        }
        [self.dataSource removeObjectsInRange:NSMakeRange(0, 5)];

        [self createHeaderView];

        [self.classificationPageTV reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
- (void)createHeaderView
{
    LJLClassificationPageModel *model = [[LJLClassificationPageModel alloc] init];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 192.5 * FITHEIGHT)];
    
    self.leftIV = [[UIImageView alloc] initWithFrame:CGRectMake(10 * FITWIDTH, 10 * FITHEIGHT, 172.5 * FITWIDTH, 172.5 * FITHEIGHT)];
    [headerView addSubview:_leftIV];
    UIButton *leftB = [UIButton buttonWithType:UIButtonTypeCustom];
    leftB.frame = self.leftIV.frame;
    [leftB addTarget:self action:@selector(didClickedToBook) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:leftB];
    
    self.midUpIV = [[UIImageView alloc] initWithFrame:CGRectMake(192.5 * FITWIDTH, 10 * FITHEIGHT, 81.25 * FITWIDTH, 81.25 * FITHEIGHT)];
    [headerView addSubview:_midUpIV];
    UIButton *midUpB = [UIButton buttonWithType:UIButtonTypeCustom];
    midUpB.frame = self.midUpIV.frame;
    [midUpB addTarget:self action:@selector(didClickedToMusic) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:midUpB];
    
    self.midDownIV = [[UIImageView alloc] initWithFrame:CGRectMake(192.5 * FITWIDTH, 101.25 * FITHEIGHT, 81.25 * FITWIDTH, 81.25 * FITHEIGHT)];
    [headerView addSubview:_midDownIV];
    UIButton *midDownB = [UIButton buttonWithType:UIButtonTypeCustom];
    midDownB.frame = self.midDownIV.frame;
    [midDownB addTarget:self action:@selector(didClickedToEntertainment) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:midDownB];
    
    self.rightUpIV = [[UIImageView alloc] initWithFrame:CGRectMake(283.75 * FITWIDTH, 10 * FITHEIGHT, 81.25 * FITWIDTH, 81.25 * FITHEIGHT)];
    UIButton *rightUpB = [UIButton buttonWithType:UIButtonTypeCustom];
    rightUpB.frame = self.rightUpIV.frame;
    [rightUpB addTarget:self action:@selector(didClickedToComic) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_rightUpIV];
    [headerView addSubview:rightUpB];
    
    self.rightDownIV = [[UIImageView alloc] initWithFrame:CGRectMake(283.75 * FITWIDTH, 101.25 * FITHEIGHT, 81.25 * FITWIDTH, 81.25 * FITHEIGHT)];
    UIButton *rightDownB = [UIButton buttonWithType:UIButtonTypeCustom];
    rightDownB.frame = self.rightDownIV.frame;
    [rightDownB addTarget:self action:@selector(didClickedToKid) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_rightDownIV];
    [headerView addSubview:rightDownB];
    
    if (self.headerDataSource.count) {
        for (NSInteger i = 0; i < 5; i++) {
            model = [self.headerDataSource objectAtIndex:i];
            switch (i) {
                case 0:
                    [self.leftIV sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
                    break;
                case 1:
                    [self.midUpIV sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
                    break;
                case 2:
                    [self.midDownIV sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
                    break;
                case 3:
                    [self.rightUpIV sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
                    break;
                case 4:
                    [self.rightDownIV sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
                    break;
                default:
                    break;
            }
        }
    }
    self.classificationPageTV.tableHeaderView = headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count / 6 + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120 * FITHEIGHT;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    __weak LJLClassificationPageViewController *weakSelf = self;
    LJLClassificationPageTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"LJLclassificationpageTVC"];
    cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    NSMutableArray *arr = [NSMutableArray array];
    switch (indexPath.row) {
        case 0:
            if (self.dataSource.count) {
                for (NSInteger i = 0; i < 6; i++) {
                    [arr addObject:self.dataSource[i]];
                };
                cell.dataSource = arr;
                cell.cBlock = ^(NSInteger albumId ,NSString *titleString)
                {
                    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
                    lnvc.categoryId = albumId;
                    lnvc.titleString = titleString;
                    lnvc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:lnvc animated:YES];
                };
            }
            break;
        case 1:
            if (self.dataSource.count) {
                [arr removeAllObjects];
                [cell.dataSource removeAllObjects];
                for (NSInteger i = 6; i < 12; i++) {
                    [arr addObject:self.dataSource[i]];
                };
                cell.dataSource = arr;
                cell.cBlock = ^(NSInteger albumId ,NSString *titleString)
                {
                    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
                    lnvc.categoryId = albumId;
                    lnvc.titleString = titleString;
                    lnvc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:lnvc animated:YES];
                };
            }
            break;
        case 2:
            if (self.dataSource.count) {
                [arr removeAllObjects];
                [cell.dataSource removeAllObjects];
                for (NSInteger i = 12; i < 18; i++) {
                    [arr addObject:self.dataSource[i]];
                };
                cell.dataSource = arr;
                cell.cBlock = ^(NSInteger albumId ,NSString *titleString)
                {
                    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
                    lnvc.categoryId = albumId;
                    lnvc.titleString = titleString;
                    lnvc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:lnvc animated:YES];
                };
            }
            break;
        case 3:
            if (self.dataSource.count) {
                [arr removeAllObjects];
                [cell.dataSource removeAllObjects];
                for (NSInteger i = 18; i < 23; i++) {
                    [arr addObject:self.dataSource[i]];
                };
                cell.dataSource = arr;
                cell.cBlock = ^(NSInteger albumId ,NSString *titleString)
                {
                    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
                    lnvc.categoryId = albumId;
                    lnvc.titleString = titleString;
                    lnvc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:lnvc animated:YES];
                };
            }
            break;
        default:
            break;
    }
    return cell;
}
#pragma mark -- 有声书
- (void)didClickedToBook
{
    LJLClassificationPageModel *model = [self.headerDataSource objectAtIndex:0];
    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
    lnvc.categoryId = model.id;
    lnvc.titleString = model.title;
    lnvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lnvc animated:YES];
}
#pragma mark -- 音乐
- (void)didClickedToMusic
{
    LJLClassificationPageModel *model = [self.headerDataSource objectAtIndex:1];
    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
    lnvc.categoryId = model.id;
    lnvc.titleString = model.title;
    lnvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lnvc animated:YES];
}
#pragma mark -- 娱乐
- (void)didClickedToEntertainment
{
    LJLClassificationPageModel *model = [self.headerDataSource objectAtIndex:2];
    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
    lnvc.categoryId = model.id;
    lnvc.titleString = model.title;
    lnvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lnvc animated:YES];
}
#pragma mark -- 评书
- (void)didClickedToComic
{
    LJLClassificationPageModel *model = [self.headerDataSource objectAtIndex:3];
    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
    lnvc.categoryId = model.id;
    lnvc.titleString = model.title;
    lnvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lnvc animated:YES];
}
#pragma mark -- 儿童
- (void)didClickedToKid
{
    LJLClassificationPageModel *model = [self.headerDataSource objectAtIndex:4];
    ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
    lnvc.categoryId = model.id;
    lnvc.titleString = model.title;
    lnvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lnvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
